﻿(function ($) {

    function remove_layertips(obj) {
        var layer_index = obj.attr('layer_index');
        if (layer_index != "" && layer_index != undefined && layer_index != null) {
            layer.close(layer_index);
        }
    }
    function validateInit(obj, rules, messages) {
        $(obj).validate({
            onkeyup: function (element) { $(element).valid(); },//切记这里设置true 是会报错的
            focusCleanup: true,
            onfocusout: function (element) { $(element).valid(); },
            rules: rules,
            messages: messages,
            submitHandler: function (obj) {
                $(obj).ajaxSubmit(function (mg) {
                    var jsonData = $.parseJSON(mg);
                    Success(jsonData);
                });
                //return true;
            },
            success: function (element) {
                var elem = $(element);
                var itemId = elem[0].htmlFor;
                var input_validate = $("#" + itemId);
                remove_layertips(input_validate);
            },
            errorPlacement: function (error, element) {
                var elem = $(element);
                if (!error.is(':empty')) {
                    var errorlist = elem.filter(':not(.valid)');
                    //alert(errorlist);
                    errorlist.each(function () {
                        remove_layertips($(this));
                        index = layer.tips(error[0].innerText, $(this), { tips: [2, '#acacac'], time: 0, tipsMore: true, zIndex: 2000, style: ['background-color:#acacac;'] });
                        $(this).attr('layer_index', index);
                    });
                }
            },
        });
    }
    $.extend($, {
        my_validate: function (obj, rules, messages) {
            return validateInit(obj, rules, messages);
        },
        check_validate: function (obj) {
            return $(obj).valid();
        }
    });
}(jQuery))